package com.zmn.oms.api.controller.order;

import com.zmn.oms.api.listener.OrderChangeNotifyFactory;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 类描述：自助同步订单状态Controller
 *
 * @author lhl
 * @date 2020/06/10 14:14
 */
@RestController
@RequestMapping("/selfhelp/order")
@Slf4j
public class SelfHelpSyncOrderController {

    @Autowired
    private OrderWorkService orderWorkService;

    /**
     * 订单状态同步
     * @return
     */
    @GetMapping("/sync")
    public String syncOrderById(String sign, Long orderId) {
        if ("0e39fd66fa4b4b239a1c240815103dbf".equals(sign)) {
            String orderStr = String.valueOf(orderId);
            try {
                log.info("【手动同步订单】收到订单同步请求：orderId=[{}]", orderStr);

                OrderWork dbOrderWork = orderWorkService.findOrderWorkByKey(orderId, orderId);
                if (dbOrderWork == null) {
                    log.info("【手动同步订单】订单不存在：orderId=[{}]", orderStr);
                    return String.format("同步失败，订单不存在：orderId=[%s]", orderStr);
                }

                OrderWorkLogChange orderWorkLogChange = new OrderWorkLogChange();
                orderWorkLogChange.setOrderId(orderId);
                orderWorkLogChange.setWorkId(dbOrderWork.getWorkId());
                orderWorkLogChange.setChannelId(dbOrderWork.getChannelId());
                orderWorkLogChange.setInputType(OrderConsts.ORDER_INPUT_TYPE_API);

                do {
                    Integer workStatus = dbOrderWork.getStatus();
                    Integer workResultStatus = dbOrderWork.getResultStatus();

                    orderWorkLogChange.setStatus(workStatus);
                    orderWorkLogChange.setResultStatus(workResultStatus);

                    if (workStatus >= OrderStatusConsts.WORK_STATUS_COMPLETE) {
                        orderWorkLogChange.setLogType(OrderLogConsts.ORDER_LOG_TYPE_COMPLETE);
                        break;
                    }

                    if (Objects.equals(workResultStatus, OrderStatusConsts.WORK_RESULT_FAIL)
                            || Objects.equals(workResultStatus, OrderStatusConsts.WORK_RESULT_DISCARD)) {
                        orderWorkLogChange.setLogType(OrderLogConsts.ORDER_LOG_TYPE_CANCEL);
                        break;
                    }

                    log.info("【手动同步订单】订单未完成：orderId=[{}]", orderStr);
                    return String.format("同步失败，订单未完成：orderId=[%s]", orderStr);
                } while (false);

                OrderChangeNotifyService orderChangeNotifyService = OrderChangeNotifyFactory.getInstance(orderWorkLogChange);
                if (orderChangeNotifyService != null) {
                    orderChangeNotifyService.notifyChange(orderWorkLogChange);
                }
                else {
                    String channelIsStr = String.valueOf(dbOrderWork.getChannelId());
                    log.info("【手动同步订单】没找到渠道对应的同步器：channelId=[{}]", channelIsStr);
                    return String.format("同步失败，没找到渠道对应的同步器：channelId=[%s]", channelIsStr);
                }

                return String.format("同步完成：orderId=[%s]", orderStr);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return String.format("同步失败，%s： orderId=[%s]", e.getMessage(), orderStr);
            }
        }
        else {
            return "Who are you???";
        }
    }

}
